package com.skycode6.servlet;

import com.skycode6.bean.AdminBean;
import com.skycode6.dao.AdminDao;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/newAdd.jsp")
public class AdminAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String adminName = req.getParameter("AdminName");
        String adminPwd = req.getParameter("AdminPwd");
        String adminType = req.getParameter("AdminType");
        AdminBean adminBean = new AdminBean();
        //设置新闻对象属性值
        adminBean.setAdminName(adminName);
        adminBean.setAdminPwd(adminPwd);
        adminBean.setLastLoginTime( (new Date()).toString() );
        adminBean.setAdminType(Integer.parseInt(adminType));
        AdminDao adminDao = new AdminDao();
        int result = adminDao.add(adminBean);
        if (result > 0) {
            //补充知识 跳转到网页news.html中
            resp.sendRedirect("news.html");//使用结果对象 完成页面网址跳转
        }else {//失败显示错误提示页面
            req.getRequestDispatcher("WEB-INF/jsp/error.jsp").forward(req, resp);
        }
    }
}
